package org.biz.modules.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

/**
 *
 * @author zf
 * @ClassName LoginUserVO
 */
@Data
@JsonIgnoreProperties({"enabled","accountNonExpired", "accountNonLocked", "credentialsNonExpired", "authorities","password","username"})
public class LoginUserVO implements Serializable, UserDetails {

   /** 用户信息 **/

   private SysUser sysUser;

   private String token;

   /** 角色信息 **/
   private List<SysRole> roleList;

   private Collection<? extends GrantedAuthority> authorities;
   public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
      this.authorities = authorities;
   }

   @Override
   public Collection<? extends GrantedAuthority> getAuthorities() {
      List<GrantedAuthority> authorities = new ArrayList<>();
      if (roleList != null) {
         List<GrantedAuthority> roleCodeList = roleList.stream().map(role -> new SimpleGrantedAuthority(role.getRoleCode())).collect(Collectors.toList());
         authorities.addAll(roleCodeList);

      }
      return authorities;
   }

   @Override
   public String getPassword() {
      return "{noop}" + (sysUser.getPwd() != null ? sysUser.getPwd() : sysUser.getIdCard());
   }

   @Override
   public String getUsername() {
      return sysUser.getName();
   }

   @Override
   public boolean isAccountNonExpired() {
      return true;
   }

   @Override
   public boolean isAccountNonLocked() {
      return true;
   }

   @Override
   public boolean isCredentialsNonExpired() {
      return true;
   }

   @Override
   public boolean isEnabled() {
      return true;
   }
}
